﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace AdventureWorksWebApp
{
    public partial class addnew : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                loadDropDown();
        }

        protected void lnkAdd_Click(object sender, EventArgs e)
        {
            SaveData();
        }

        protected void ddCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (DataClasses1DataContext dc = new DataClasses1DataContext())
            {
                ddSubcategory.Items.Clear();
                var list = from sub in dc.ProductSubcategories
                           where sub.ProductCategoryID == int.Parse(ddCategory.SelectedItem.Value)
                           select sub;
                foreach (ProductSubcategory sub in list)
                    ddSubcategory.Items.Add(new ListItem(sub.Name, sub.ProductSubcategoryID.ToString()));
            }
        }

        private void loadDropDown()
        {
            using (DataClasses1DataContext dc = new DataClasses1DataContext())
            {
                var list2 = from cat in dc.ProductCategories
                            select cat;
                ddCategory.Items.Clear();
                foreach (ProductCategory cat in list2)
                    ddCategory.Items.Add(new ListItem(cat.Name, cat.ProductCategoryID.ToString()));

                var list = from sub in dc.ProductSubcategories
                           where sub.ProductCategoryID == int.Parse(ddCategory.SelectedItem.Value)
                           select sub;
                ddSubcategory.Items.Clear();
                foreach (ProductSubcategory sub in list)
                    ddSubcategory.Items.Add(new ListItem(sub.Name, sub.ProductSubcategoryID.ToString()));
            }
        }

        private void SaveData()
        {
            using (DataClasses1DataContext dc = new DataClasses1DataContext())
            {
                Product produkt = new Product();
                produkt.Name = txtProductName.Text;
                produkt.ProductNumber = txtProductNumber.Text; // MUSI BYC UNIQUE
                produkt.DaysToManufacture = int.Parse(txtDaysToManufacture.Text);
                produkt.ListPrice = decimal.Parse(txtListPrice.Text);
                produkt.ReorderPoint = short.Parse(txtReorderPoint.Text);
                produkt.rowguid = Guid.Parse(txtRowGUID.Text);
                produkt.SafetyStockLevel = short.Parse(txtSafetyStockLevel.Text);
                produkt.StandardCost = decimal.Parse(txtStandardCost.Text);
                produkt.FinishedGoodsFlag = chbFinishedGoodsFlag.Checked;
                produkt.MakeFlag = chbMakeFlag.Checked;
                produkt.ProductSubcategoryID = int.Parse(ddSubcategory.SelectedItem.Value);
                produkt.SellStartDate = DateTime.Now;
                produkt.ModifiedDate = DateTime.Now;

                dc.Products.InsertOnSubmit(produkt);
                dc.SubmitChanges();
            }
        }

        protected void lnkGenerate_Click(object sender, EventArgs e)
        {
            string guid = generateGUID();
            txtRowGUID.Text = Guid.Parse(guid).ToString();
        }

        private string generateGUID()
        {
            string guid ="";
            string chars = "1234567890";
            char[] allowedchars = chars.ToCharArray();
            Random r = new Random();
            for (int x = 0; x < 32; x++)
            {
                
                guid += allowedchars[r.Next(allowedchars.Count())];
            }
            return guid;
        }
    }
}